export const cleanObject = (object: { [key: string]: unknown }) => {
  const result = { ...object };
  Object.keys(result).forEach((key) => {
    const value = result[key];
    if (!value) {
      delete result[key];
    }
  });
  return result;
};

/**
 * 将对象类型转换为 formData类型
 */
export const dataToFormData = (data: { [key in string]: Blob | Blob[] | File | File[] | string | string[] }) => {
  const formData = new FormData();
  Object.keys(data).forEach((k) => {
    const value = data[k];
    if (Array.isArray(value)) {
      value.forEach((v) => {
        formData.append(k, v);
      });
    } else {
      formData.append(k, value);
    }
  });

  return formData;
};

export const questionBase64Split = (text: string): string[] => {
  const newOption = text.replaceAll('base64,', 'base64');
  const arr = newOption.split(',');
  arr.forEach((e, index) => {
    if (e.indexOf('base64') !== -1) {
      const temp = e.replaceAll('base64', 'base64,');
      arr[index] = temp;
    }
  });
  return arr;
};
